<?php

    /**
     * Class Navigate_model
     */
    class P
    {
        static public $CI;

        function __construct()
        {
            if ( !self::$CI ) {
                self::$CI =& get_instance();
            }
        }

        /**接口使用
         *
         * @param string $data
         *
         * @return array
         */
        static function returnOk( $data = '' )
        {
            return [ 'isok' => true, 'error' => '', 'data' => $data ];
        }

        /**
         * 服务层统一返回
         *
         * @param null $data要返回的数据
         * @param null $rows要返回的总行数
         * @param null $error_msg返回的错误信息
         * @param null $error_code返回的状态码
         *
         * @return array
         */
        static function returnSuccess( $data = null, $rows = null, $error_msg = null, $error_code = null )
        {
            return [ 'isok' => true, 'data' => $data, 'rows' => $rows, 'error_msg' => $error_msg, 'error_code' => $error_code ];
        }

        /**
         * 服务层统一返回
         *
         * @param null $error_msg返回的错误信息
         * @param null $error_code返回的错误状态码
         * @param null $data返回的数据
         *
         * @return array
         */
        static function returnFail( $error_msg = null, $error_code = null, $data = null )
        {
            return [ 'isok' => false, 'error_msg' => $error_msg, 'error_code' => $error_code, 'data' => $data ];
        }

        /**传入参数错误
         * @param null $error_msg
         * @param null $error_code
         * @param null $data
         *
         * @return array
         */
        static function returnArgsError( $error_msg = null, $error_code = null, $data = null )
        {
            $error_msg = '传入'.$error_msg.'参数错误';
            if ( !$error_code ) $error_code = 'ARGS_ERROR';

            return self::returnFail( $error_msg, $error_code, $data );
        }

        /**查询没有数据
         * @param null $error_msg
         * @param null $error_code
         * @param null $data
         *
         * @return array
         */
        static function returnNoData( $error_msg = null, $error_code = null, $data = null )
        {
            $error_msg .= '没有数据';
            if ( !$error_code ) $error_code = 'NO_DATA';

            return self::returnFail( $error_msg, $error_code, $data );
        }

        /**接口使用
         *
         * @param string $error
         * @param string $error_code
         * @param string $data
         *
         * @return array
         */
        static function returnError( $error = '', $error_code = '', $data = '', $location = '' )
        {
            return [ 'isok' => false, 'error' => $error, 'error_code' => $error_code, 'data' => $data, 'location' => $location ];
        }

        /**配全easyui使用
         *
         * @param string $rows
         * @param string $total
         */
        static function exitOk( $rows = '', $total = '' )
        {
            self::adminLog( 1 );
            exit( json_encode( [ 'isok' => true, 'rows' => $rows, 'total' => $total ], JSON_UNESCAPED_UNICODE ) );
        }

        /**
         * @param string $error
         * @param string $error_code
         * @param string $location
         * @param string $result
         */
        static function exitError( $error = "", $error_code = '', $location = '', $data = '' )
        {
            if ( is_array( $error ) ) {
                $error_arr = $error;
                $error = $error_arr[ 'error' ];
                $error_code = $error_arr[ 'error_code' ];
                $location = $error_arr[ 'location' ];
                $data = $error_arr[ 'data' ];
            }
            self::adminLog( 0, $error );
            exit( json_encode( [ 'isok' => false, 'error' => $error, 'error_code' => $error_code, 'location' => $location, 'data' => $data ], JSON_UNESCAPED_UNICODE ) );
        }

        static function exitAll( array $exit_info )
        {
            if ( $exit_info[ 'isok' ] ) {
                $rows = [ ];
                $total = '';
                if ( isset( $exit_info[ 'data' ][ 'rows' ] ) )
                    $rows = $exit_info[ 'data' ][ 'rows' ];
                if ( isset( $exit_info[ 'data' ][ 'total' ] ) )
                    $total = $exit_info[ 'data' ][ 'total' ];
                self::exitOk( $rows, $total );
            } else
                self::exitError( $exit_info );
        }

        static function lastsql()
        {
            $last_sql = self::$CI->db->last_query();
            self::p( $last_sql );
        }

        /**后台记录日志
         *
         * @param string $isok
         * @param string $error
         *
         * @return bool
         */
        static function adminLog( $isok = '', $error = '' )
        {
            //$ci = &get_instance();//ci控制器超级对象，就是把所有执行的方法打印出来
            $ci = self::$CI;
            $arr[ 'option_directory' ] = substr( $ci->router->directory, 0, -1 ); //分组目录
            if ( strtolower( $arr[ 'option_directory' ] ) != 'admin' )
                return false;
            $arr[ 'admin_id' ] = isset( $_SESSION[ 'admin_id' ] ) ? $_SESSION[ 'admin_id' ] : '';
            $arr[ 'admin_name' ] = isset( $_SESSION[ 'admin_name' ] ) ? $_SESSION[ 'admin_name' ] : '';
            $arr[ 'option_controller' ] = $ci->router->class;   //当前控制器
            $arr[ 'option_method' ] = $ci->router->method;    // 当前使用方法
            $arr[ 'option_url' ] = $_SERVER[ 'PHP_SELF' ];
            $arr[ 'option_get' ] = json_encode( $_GET, JSON_UNESCAPED_UNICODE );
            $arr[ 'option_post' ] = json_encode( $_POST, JSON_UNESCAPED_UNICODE );
            $arr[ 'option_time' ] = date( "Y-m-d H:i:s" );
            $arr[ 'option_ip' ] = $ci->input->ip_address();
            $arr[ 'status' ] = $isok;
            $arr[ 'desc' ] = $error;
            $ci->db->insert( 'admin_log', $arr );
        }

        static function p( $str, $pre_info = "", $last_info = "", $style = 0 )
        {
            if ( !$style )
                echo "\n";
            echo $pre_info;
            print_r( $str );
            echo $last_info;
            exit();
        }

        static function v( $str, $pre_info = "", $last_info = "", $style = 0 )
        {
            if ( !$style )
                echo "\n";
            echo $pre_info;
            print_r( $str );
            echo $last_info;
            echo "\n";
        }
    }

?>